package com.sm.chatinputlibrary.emoji;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scorpio.baselibrary.adapter.BasicPagerAdapter;
import com.scorpio.baselibrary.utils.ViewController;
import com.sm.chatinputlibrary.R;

import java.util.ArrayList;
import java.util.List;

/**
 * created by zhud on 2019/4/2
 */
public class EmojiPageAdapter extends BasicPagerAdapter implements BaseQuickAdapter.OnItemChildClickListener {
    private List<EmojiPageBean> beans;

    public EmojiPageAdapter(List<EmojiPageBean> beans) {
        this.beans = beans;
    }

    public void setData(List<EmojiPageBean> beans) {
        this.beans = beans;
        notifyDataSetChanged();
    }

    @Override
    public View onCreateView(ViewGroup container) {
        View view = LayoutInflater.from(container.getContext()).inflate(R.layout.input_layout_recycler_view, container, false);
        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
//        recyclerView.setPadding(30,0,30,0);
        recyclerView.setPadding(0, 20, 0, 0);
        recyclerView.setLayoutManager(new GridLayoutManager(container.getContext(), 6));
        EmojiLiseAdapter adapter = new EmojiLiseAdapter(new ArrayList<EmojiBean>());
        adapter.setOnItemChildClickListener(this);
        recyclerView.setAdapter(adapter);
        return view;
    }

    @Override
    public void onBindView(ViewController holder, int position) {
        RecyclerView recyclerView = holder.getView(R.id.recyclerView);
        EmojiLiseAdapter emojiLiseAdapter = (EmojiLiseAdapter) recyclerView.getAdapter();
        emojiLiseAdapter.setNewData(beans.get(position).getBeans());
        emojiLiseAdapter.notifyDataSetChanged();
    }

    @Override
    public boolean isViewHolderChanged(ViewController holder) {
        return false;
    }

    @Override
    public int getCount() {
        return beans == null ? 0 : beans.size();
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        if(onItemEmojiChildClickListener!=null){
            List<EmojiBean> emojiBeans = adapter.getData();
            EmojiBean bean = emojiBeans.get(position);
            onItemEmojiChildClickListener.onItemChildClick(bean);
        }
    }

    private OnItemEmojiChildClickListener onItemEmojiChildClickListener;

    public void setOnItemEmojiChildClickListener(OnItemEmojiChildClickListener onItemEmojiChildClickListener) {
        this.onItemEmojiChildClickListener = onItemEmojiChildClickListener;
    }

   public interface OnItemEmojiChildClickListener {
        void onItemChildClick(EmojiBean bean);
    }
}